home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / patch-21.lha / patch-2.1 / rename.c < prev    next >
C/C++ Source or Header  |  1992-07-06  |  1KB  |  52 lines

  1. /* rename.c -- BSD compatible directory function for System V
  2.    Copyright (C) 1988, 1990, 1992 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include <sys/types.h>
  19. #include <sys/stat.h>
  20. #include <errno.h>
  21. #ifndef STDC_HEADERS
  22. extern int errno;
  23. #endif
  24.  
  25. /* Rename file FROM to file TO.
  26.    Return 0 if successful, -1 if not. */
  27.  
  28. int
  29. rename (from, to)
  30.      char *from;
  31.      char *to;
  32. {
  33.   struct stat from_stats;
  34.  
  35.   if (stat (from, &from_stats))
  36.     return -1;
  37.  
  38.   if (unlink (to) && errno != ENOENT)
  39.     return -1;
  40.  
  41.   if (link (from, to))
  42.     return -1;
  43.  
  44.   if (unlink (from) && errno != ENOENT)
  45.     {
  46.       unlink (to);
  47.       return -1;
  48.     }
  49.  
  50.   return 0;
  51. }
  52.